package com.exampleyyy.demoyyy.common;

import com.alibaba.fastjson.JSON;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.exampleyyy.demoyyy.ShiTi.Zhifubao;
import org.springframework.stereotype.Component;

@Component//注解 做成一个组件类
public class Alipay {
    public String pay(Zhifubao zhifubao) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        // 创建配置对象
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCoquM1IUa+2R82KA+W3z/TOCuwsmFoue9ZYnx09XxgouGXoidXiTUC0WWYJERy8tveH3ovdjERPGnuDAYJ1a3TAUapKL2vjIWnK1+2WKr/33XEbGX2Q9eLIYe2lDP0jTpHit2X2a/G/9sTfALdnPMSOtcFemYL3eABI97uuqS8lc66lIfwL23mP33sRjv0h+08FGzibSM3uY9rwiGU4m3VdONfjqsOhUpljbXrYqh8Y313KJUHhWmcebiAuId5vHMQ0mD9Ya/32rj4InQpRAbjU+vGn2vnRqRJbav4ZozZ3hYfo8CuDEsES4hY6frJD1fucnpAsCaHNvjwulta45pbAgMBAAECggEAfEcWDNLNDthqn/vaqL8shxSPVK3LCmBeVM01iPRMxeOeN506QcyOa9ogcn7elJgPm1Y0MH503w9VuhnP5TU11BDPoNqtEYmH/Un/RxuEO+7KFoYMhu0sB48YMVZ4Bcx90bMnXbj/RLDHGk84yVPY4mjKgHXUdQ/x+sUUgXCy62SAOJQC5K7g26T0526IQ1TyAcEwe3WSl7qTmWBRB/p+g8n1erP6VQQGXXjZYVVQSEK3H31P/8jq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55wC/aoL8uN0vXAJt5x9/kxly0w1mByDT12kpslLxahUZTbjWm4j3G+82E4sBPWxTVZ1xjk3bkmNzc5hc6mqxtAAVok+78mpQuyWKVk+SZHV30PhXCTVO1dO9lNLiTMR/03zm7HflvNEt2aHiS7tDZJt6aB9eD2GUHRMHGDtxu2eBrQa9MhhDiaJ7mZqjP6DjQPLJRMcHWL+lapi010vWlowaB2UJcW74tSpS+oeM58uibigtZBggrPOPiWetqX+xpIci2cIhVeLANVYSBmYdz4O0nIdm1E6HiKOrNGPXhGNOFA0T+g/UrWPjwIDAQAB";// 自己的公钥
        String APPID = "9021000140660956";// 自己的应用编号
        // String returnUrl = "http://127.0.0.1:8081/front/page/pay-success.html";
        String returnUrl = "http://8.130.117.8:8081/front/page/pay-success.html";
        String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(APPID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);

        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        // 创建支付客户端对象
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        // 创建手机浏览器请求对象,用来请求打开支付宝app

        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        // 手机浏览器支付 ,拉起支付宝app
        model.setOutTradeNo(zhifubao.getOut_trade_no());// 订单编号
        model.setTotalAmount(zhifubao.getTotal_amount()); // 订单金额
        model.setSubject(zhifubao.getSubject());// 订单主题
        model.setProductCode("QUICK_WAP_WAY"); // 产品编号,手机端浏览器支付,固定,不需要修改
        model.setSellerId("hawqkq9129@sandbox.com"); // 商家支付宝账号
        // 注意:一定要用用商家的支付宝账号
        request.setBizModel(model);//将数据设置到请求对象中
        request.setReturnUrl(returnUrl); // 设置支付成功之后跳转的页面

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        // 发起支付请求
        String result = response.getBody();
        // 返回支付后的响应结果
        return result;
    }
}
